﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using logic;

namespace PeisongWeb.admin
{
    public partial class salesPercentageList : adminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (hasPermission("GE", 1))
                {
                    drpBind();
                    rptBind();
                }
                else
                {
                    Library.Script.ClientMsgUrl("没有权限！", "index.aspx");
                    return;
                }
            }
        }
        private void drpBind()
        {
            DataTable dt = logic.sysAdmin.listWithSalesperson();
            ddl_Sales.DataSource = dt;
            ddl_Sales.DataBind();
        }

        private void rptBind()
        {
            int saleid = 0;
            int.TryParse(ddl_Sales.SelectedValue, out saleid);
            string year = ddlYear.SelectedValue;
            DataTable dt = logic.percentageDate.listWithSaleAndYear(saleid, int.Parse(year));
            rpt_list.DataSource = dt;
            rpt_list.DataBind();
        }

        protected void btn_Create_Click(object sender, EventArgs e)
        {
            int saleid = 0;
            int.TryParse(ddl_Sales.SelectedValue, out saleid);
            string year = ddlYear.SelectedValue;

            if (logic.percentageDate.isExist(int.Parse(year),saleid))
            {
                Library.Script.ClientMsg("当前销售人员在"+year+"年已经初始化了！");
                return;
            }
            else
            {
                logic.percentageDate.insert(saleid, int.Parse(year), logic.sysAdmin.AdminID);
                Library.Script.ClientMsg("初始化成功！");
            }
            rptBind();
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            rptBind();
        }

        protected void rpt_list_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Calculate")
            {
                int did = Convert.ToInt32(e.CommandArgument);
                int flag = salesPercentage.calculatePercentage(did,logic.sysAdmin.AdminID);
                if (flag>0)
                {
                    percentageListBind(did);
                }
                else
                {
                    Library.Script.ClientMsg("当前销售员在此季度没有提成！");
                }
            }
            if (e.CommandName == "Select")
            {
                int did = Convert.ToInt32(e.CommandArgument);
                percentageListBind(did);
            }
        }

        private decimal totalMoney = 0m;
        private decimal sumSeason = 0m;
        private decimal sumYearEnd = 0m;
        private void percentageListBind(int did)
        {
            string year = ddlYear.SelectedValue;
            lbl_year.Text = year + "年";
            DataSet ds = logic.salesPercentage.select("did=" + did, "spid", 100, 1);
            DataRow dr = ds.Tables[1].Rows[0];
            string salesName = lbl_salesName.Text = dr["username"].ToString();
            sale_Percentage.Title = salesName + "的提成详情";
            lbl_startDt.Text = Convert.ToDateTime(dr["startDt"].ToString()).ToShortDateString();
            lbl_endDt.Text = Convert.ToDateTime(dr["endDt"].ToString()).ToShortDateString();
            rpt_PercentageList.DataSource = ds.Tables[1];
            rpt_PercentageList.DataBind();
            sale_Percentage.Show();
            foreach (RepeaterItem item in rpt_PercentageList.Items)
            {
                if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
                {
                    Literal lbl = item.FindControl("lbl_amount") as Literal;

                    if (lbl != null)
                    {
                        totalMoney += decimal.Parse(lbl.Text);
                    }

                    Literal ltls = item.FindControl("ltl_sp") as Literal;

                    if (ltls != null)
                    {
                        sumSeason += decimal.Parse(ltls.Text);
                    }

                    Literal ltly = item.FindControl("ltl_yp") as Literal;

                    if (ltly != null)
                    {
                        sumYearEnd += decimal.Parse(ltly.Text);
                    }
                }
            }
            lbl_sumSeason.Text = string.Format("{0:F2}", sumSeason);
            lbl_sumYearEnd.Text = string.Format("{0:F2}", sumYearEnd);
            lbl_sum.Text = string.Format("{0:F2}", totalMoney);
        }
    }
}
